package webserver.src;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Vector;
import java.util.Hashtable;
import java.util.StringTokenizer;
import java.lang.*;
import java.util.HashMap;

/**
 * <p>Title: HttpdConf.java</p>
 *
 * <p>Description: This class will configure your server to the specifications
 * found within the httpd.conf file. </p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: </p>
 * @author Luping May and Tracie Hong
 * @version 1.0
 */
public class HttpdConf {
    /*private String serverRoot;
    private String serverAdmin;
    private String documentRoot;
    private String listen;
    private String logFile;
    private String accessFileName;
    private String scriptAlias;
    private String directoryIndex;
     */
    private int maxThreaads;
    private HashMap <String, String> httpMap;
    private HashMap <String, String> scriptAlias;
    private HashMap <String, String> alias;

  /**
   * Default constructor to reset your variables and data structures.
   */
  public HttpdConf(){
    //Initialize httpMap
    httpMap = new HashMap<String, String>();
    httpMap.put("serverRoot", null);    //add key for serverRoot
    httpMap.put("serverAdmin", null);   //add key for serverAdmin
    httpMap.put("documentRoot", null);  //add key for documentRoot
    httpMap.put("listen", null);        //add key for listen
    httpMap.put("logFile", null);       //add key for logFile
    httpMap.put("accessFileName", null);//add key for accessFileName
    //httpMap.put("scriptAlias", null);   //add key for scriptAlias
    httpMap.put("directoryIndex", null);//add key for directoryIndex

    //Initialize alias
    alias = new HashMap<String, String>();

    //Initialize scriptAlias
    scriptAlias = new HashMap<String, String>();
  }

  /**
   * Reads in a httpd.conf file, parses it and saves the data stored within that
   * file. This allows for proper configuration of your server since the
   * information stored in your configuration file should allow for your server
   * to function.
   *
   * @param path path to your httpd.conf file
   */
  public void readHttpd(String path) throws FileNotFoundException, IOException{
    BufferedReader reader = new BufferedReader(new FileReader(path + "httpd.conf"));
    while( reader.ready() )
    {
        String inputLine = reader.readLine();
        System.out.println(inputLine);
        String splits[] = inputLine.split("\\s+");
        //System.out.println(splits[0] + " " + splits[1] + " ");
        if( splits[0].equals("Alias") )
        {
            splits[2].replace("/", File.separator);
            System.out.println(splits[1] + " "  + splits[2]);
            alias.put(splits[1], splits[2]);
        }
        else if( splits[0].equals("scriptAlias") )
        {
            splits[1].replace("/", File.separator);
            scriptAlias.put(splits[0], splits[1]);
        }
        else if(httpMap.containsKey(splits[0]))
        {
            httpMap.put(splits[0], splits[1]);
        }
    }
  }

  /**
   * Function to convert aliases set within your httpd.conf file to their
   * absolute path. This allows for aliases to be found on your server and
   * returned back to the client.
   * HINT: You may find it helpful to create a private class to store your
   * aliases.
   *
   * @param fakeName String which contains the alias of the file or directory
   * @return String value which contains the absolute path to the file or
   *   directory as determined within the httpd.conf file
   */
  public String solveAlias(String fakeName){
    String tempAlias = alias.get( fakeName );
    System.out.println( tempAlias );
    tempAlias.replace("/", File.separator);
    System.out.println( tempAlias );
    return tempAlias;
  }

  /**
   * Used to read the mime.types file and save all the information from that file
   * into a data structure that can be used to validate file types when
   * generating response messages.
   *
   * @param path String value of path to mime.types file
   */
  public void readMIME (String path) {
    
  }

  /**
   * Helper function to determine whether the name of a file or directory is an
   * alias for another file or directory as noted in the httpd.conf file.
   *
   * @param name String value of the alias we want to check to determine
   *   whether it is or is not an alias for another file or directory
   * @return true if it is an alias, false otherwise
   */
  public boolean isScript(String name)	{
    return alias.containsKey(name);
  }

  /**
   * Helper function to see if you've parsed your httpd.conf file properly. Used
   * for debugging purposes.
   */
  public void testPrint(){
    /*System.out.println("ServerRoot: "+serverRoot);
    System.out.println("DocumentRoot: "+documentRoot);
    System.out.println("ListenPort: "+listen);
    System.out.println("LogFile: "+logFile);
    System.out.println("AccessFileName: " + accessFileName);
    System.out.println("ScriptAlias: "+scriptAlias+" "+solveAlias(scriptAlias));
     */
  }
}
